package com.changfeng.service.impl;

import com.changfeng.bean.ReatailersCart;
import com.changfeng.bean.ReatailersCommodity;
import com.changfeng.bean.dto.bo.CartBO;
import com.changfeng.bean.dto.vo.CartListVO;
import com.changfeng.bean.dto.vo.CartToCommodityVO;
import com.changfeng.dao.ReatailersCartMapper;
import com.changfeng.dao.ReatailersCommodityMapper;
import com.changfeng.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {


    @Autowired
    private ReatailersCartMapper reatailersCartMapper;

    @Autowired
    private ReatailersCommodityMapper reatailersCommodityMapper;

    @Override
    public void addCart(CartBO bo) {
        ReatailersCart reatailersCart = reatailersCartMapper.selectByCommodityIdAndUserId(bo);
        if (reatailersCart == null) {
            reatailersCart = new ReatailersCart();
            reatailersCart.setCommodityNumber(1);
            reatailersCart.setUserId(bo.getUserId());
            reatailersCart.setCommodityId(bo.getCommodityId());
            reatailersCart.setGmtCreate(new Date());
            reatailersCart.setGmtModified(new Date());
            reatailersCartMapper.insert(reatailersCart);
        } else {
            reatailersCart.setCommodityNumber(reatailersCart.getCommodityNumber()+1);
            reatailersCartMapper.updateByPrimaryKey(reatailersCart);
        }
    }

    @Override
    public List<CartListVO> findCartByUser(Integer userId) {
        List<ReatailersCart> reatailersCarts = reatailersCartMapper.selectByUserId(userId);
        if (reatailersCarts == null) {
            return null;
        }

        List<CartListVO> vos = new ArrayList<>();
        for (ReatailersCart cart : reatailersCarts) {
            ReatailersCommodity reatailersCommodity = reatailersCommodityMapper.selectByPrimaryKey(cart.getCommodityId());
            if (reatailersCommodity != null) {
                CartListVO cartListVO = new CartListVO();
                cartListVO.setCartId(cart.getCartId());
                cartListVO.setCommodityId(reatailersCommodity.getCommodityId());
                cartListVO.setCommodityImg(reatailersCommodity.getCommodityImgurl());
                cartListVO.setCommodityName(reatailersCommodity.getCommodityName());
                cartListVO.setCommodityNum(cart.getCommodityNumber());
                cartListVO.setCommodityPrice(reatailersCommodity.getCommodityPrice().doubleValue());
                vos.add(cartListVO);
            }
        }


        return vos;
    }

    @Override
    public void deleteCart(Integer cartId) {
        reatailersCartMapper.deleteByPrimaryKey(cartId);
    }

    @Override
    public List<CartToCommodityVO> getCommodityId(List<Integer> cartId) {
        List<ReatailersCart> reatailersCarts = reatailersCartMapper.selectByIds(cartId);
        List<CartToCommodityVO> commodity = new ArrayList<>();
        for (ReatailersCart cart : reatailersCarts) {
            CartToCommodityVO vo = new CartToCommodityVO();
            vo.setCommodityNum(cart.getCommodityNumber());
            vo.setCommodityId(cart.getCommodityId());
            commodity.add(vo);
        }
        return commodity;
    }
}
